<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Users extends Account_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('string');
        $this->load->model('user_m');
    }

    public function addUser() {
        if ($this->input->server('REQUEST_METHOD') == "POST") {

            $rules = array(
                array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'required|max_length[50]|valid_email'
                ),
                array(
                    'field' => 'password',
                    'label' => 'Mật khẩu',
                    'rules' => 'required|max_length[50]'
                ),
                array(
                    'field' => 'role_id',
                    'label' => 'Nhóm tài khoản',
                    'rules' => 'required|is_natural_no_zero|max_length[11]'
                ),
                array(
                    'field' => 'name',
                    'label' => 'Họ tên',
                    'rules' => 'required|max_length[100]'
                ),
                array(
                    'field' => 'is_active',
                    'label' => 'Trạng thái',
                    'rules' => 'required|is_natural_no_zero|max_length[2]'
                )
            );
            $this->form_validation->set_rules($rules);
            if ($this->form_validation->run() == TRUE) {
                $user = array(
                    'email' => $this->input->post('email'),
                    'password' => $this->input->post('password'),
                    'role_id' => $this->input->post('role_id'),
                    'name' => $this->input->post('name'),
                    'is_active' => $this->input->post('is_active'),
                    'created_on' => time(),
                );
                // random salt
                $user['salt'] = random_string('alnum', 5);
                $id = $this->user_m->insert($user);
                if($id) {

                } else {

                }
            }
        }
    }

    public function editUser() {
        if ($this->input->server('REQUEST_METHOD') == "POST") {

            $rules = array(
                array(
                    'field' => 'id',
                    'label' => 'Id',
                    'rules' => 'required|is_natural_no_zero'
                ),
                array(
                    'field' => 'email',
                    'label' => 'Email',
                    'rules' => 'required|max_length[50]|valid_email'
                ),
                array(
                    'field' => 'password',
                    'label' => 'Mật khẩu',
                    'rules' => 'required|max_length[50]'
                ),
                array(
                    'field' => 'role_id',
                    'label' => 'Nhóm tài khoản',
                    'rules' => 'required|is_natural_no_zero|max_length[11]'
                ),
                array(
                    'field' => 'name',
                    'label' => 'Họ tên',
                    'rules' => 'required|max_length[100]'
                ),
                array(
                    'field' => 'is_active',
                    'label' => 'Trạng thái',
                    'rules' => 'required|is_natural_no_zero|max_length[2]'
                )
            );
            $this->form_validation->set_rules($rules);
            if ($this->form_validation->run() == TRUE) {
                $id = $this->input->post('id');
                $user = array(
                    'email' => $this->input->post('email'),
                    'password' => $this->input->post('password'),
                    'role_id' => $this->input->post('role_id'),
                    'name' => $this->input->post('name'),
                    'is_active' => $this->input->post('is_active'),
                    'created_on' => time(),
                );
                // random salt
                $user['salt'] = random_string('alnum', 5);
                $rs = $this->user_m->update($id, $user);
                if($rs) {

                } else {

                }
            }
        }
    }

    public function deleteUser() {
        if ($this->input->server('REQUEST_METHOD') == "POST") {
            $rules = array(
                array(
                    'field' => 'id',
                    'label' => 'Id',
                    'rules' => 'required|is_natural_no_zero'
                )
            );
            $this->form_validation->set_rules($rules);
            if ($this->form_validation->run() == TRUE) {
                $id = $this->input->post('id');
                $rs = $this->user_m->delete($id);
                if($rs) {

                } else {

                }
            }
        }
    }
}